/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.annotation.util;

import org.balisunrise.annotation.Search;
import org.balisunrise.annotation.VisualEntity;

/**
 * Esta classe contém os atributos para construção de uma busca para uma
 * entidade.
 * @author Glauco
 */
public class SearchUtilities extends VisualEntityUtilities{

    private static final String MESSAGE = "Erro ao ler Annotation \"Search\".";

    /**
     * O alias (nome) do filtro.
     */
    public String filter;

    /**
     * O alias (nome) do grid.
     */
    public String grid;

    /**
     * FilterUtilities construido pelo alias filter.
     */
    public FilterUtilities filterUtilities;

    /**
     * GridUtilities construido pelo alias grid.
     */
    public GridUtilities gridUtilities;

    /**
     * Lê a annotation Search com o alias da VisualEntity.
     * @param entityClass Classe para ler.
     * @param alias alias da Search.
     * @return a annotation Search do alias.
     * @throws AnnotationsUtilitiesException Caso não encontre.
     */
    public static Search readSearchAnnotation(
            Class entityClass, String alias)
            throws AnnotationsUtilitiesException {

        if (entityClass == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"entityClass\" não pode ser nulo!");

        if (alias == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"alias\" não pode ser nulo!");

        VisualEntity ve = readVisualEntityAnnotation(entityClass);
        for (Search s : ve.searchs()) {
            if(s.alias().equals(alias)){
                return s;
            }
        }

        throw new AnnotationsUtilitiesException( MESSAGE +
                "\nAnnotation \"Search\" não encontrada para o "
                + "nome\""+ alias +"\"!");

    }

    /**
     * Le a annotation Search e cria um SearchUtilities a parir dos dados da
     * annotation.
     *
     * @param entityClass Classe para ler.
     * @param alias alias da Search.
     * @return SearchUtilities
     * @throws AnnotationsUtilitiesException Caso não encontre a annotation ou
     * algum parâmetro seja nulo.
     */
    public static SearchUtilities create(Class entityClass, String alias)
            throws AnnotationsUtilitiesException {

        if (entityClass == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"entityClass\" não pode ser nulo!");

        if (alias == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"alias\" não pode ser nulo!");

        Search s = readSearchAnnotation(entityClass, alias);

        SearchUtilities su = new SearchUtilities();
        su.entityClass = entityClass;
        su.alias = alias;
        su.title = s.title();
        su.filter = s.filter();
        su.grid = s.grid();

        // cria filtro
        su.filterUtilities = FilterUtilities.create(entityClass, su.filter);

        // cria grid
        su.gridUtilities = GridUtilities.create(entityClass, su.grid);

        return su;
    }
}
